var websocketID = "meinTest",
		websocketClient = null,
		wsUsername = null,
		wsPassword = null;

//Method is called when the page is loaded
function initPage() {
	// check if WebSockets are supported by the browser
	if (jws.browserSupportsWebSockets()) {
		//console.log("Websockets are supported");
		// instaniate new TokenClient, either JSON, CSV or XML
		websocketClient = new jws.jWebSocketJSONClient();
		jws.oop.addPlugIn(jws.jWebSocketTokenClient, jws.GlobisPlugin);
		logon();//call the logon Method
	} else {
		//console.log("Websockets are NOT supported");
	}
}


function logon() {
	var wsURL = jws.getDefaultServerURL();// get the default server url
	//console.log("Connecting to " + websocketID + " at " + wsURL + "...");

	//get the guest username and password
	wsUsername = jws.GUEST_USER_LOGINNAME;
	wsPassword = jws.GUEST_USER_PASSWORD;

	// try to establish the connection to jWebSocket server
	websocketClient.logon('ws://localhost:8787', wsUsername, wsPassword, {
		OnOpen: function(aEvent) {
			//console.log("Connection to " + websocketID + " established.");
		},
		// OnMessage callback
		OnMessage: function(aEvent, aToken) {

			if (aToken.type === "welcome") {

				websocketClient.register(registerType);
				//console.log((aToken ? aToken.type : "-") + ": " + aEvent.data);

			}
//			if (websocketClient.isLoggedIn()) {
//				console.log("User is authenticated");
//			} else {
//				console.log("User is connected");
//			}
			//console.log(websocketClient.getId() + "&nbsp;" + (jws.browserSupportsNativeWebSockets ? "(native)" : "(flashbridge)"));
			if (aToken) {
				// is it a response from a previous request of this client?
				if (aToken.type === "response") {
					if (aToken.reqType == "login") {// figure out of which request
						if (aToken.code == 0) {
							//console.log("Welcome '" + aToken.username + "'");
						} else {
							//console.log("Error logging in '" + eUsername.value + "': " + aToken.msg);
						}
					}
					// is it an event w/o a previous request ?
				} else if (aToken.type === "goodBye") {
					//console.log("good bye (reason: " + aToken.reason + ")!");
				}
			}
		},
		// OnClose callback
		OnClose: function(aEvent) {
			//console.log("Disconnected from " + websocketID + ".");
		}

	});
}

